(ns tess4j.core
  (:gen-class)
  (:require [clj-http.client :as client])
  (:import javax.imageio.ImageIO
           java.awt.image.BufferedImage
           java.io.File
           java.awt.Color
           java.net.URL
           java.net.HttpURLConnection
           java.io.ByteArrayInputStream))


; (def img (ImageIO/read (File. "randImg.jpg")))
(def img (ImageIO/read (URL. "https://billcloud.unionpay.com/ccfront/validate/randImg?t=0.08051690586156279")))
(def tess (net.sourceforge.tess4j.Tesseract.))
(def co-black (.getRGB Color/BLACK))
(def co-white (.getRGB Color/WHITE))
; (def bimg (BufferedImage. 75 28 BufferedImage/TYPE_BYTE_BINARY))
(def fcolor (Color. (.getRGB img 0 0)))
(def black-color (Color. co-black))
; (def colornum (+ (.getRed fcolor) (.getGreen fcolor) (.getBlue fcolor) 20))
(def colornum 100)
(defn process-image [img]
  (let [timg (BufferedImage. 75 28 BufferedImage/TYPE_BYTE_BINARY)]
    (doall
      (for [x (range 0 75)]
        (.setRGB timg x 0 co-white)))
    (doall
      (for [x (range 0 75)]
        (.setRGB timg x 27 co-white)))
    (doall
      (for [y (range 0 28)]
        (.setRGB timg 0 y co-white)))
    (doall
      (for [y (range 0 28)]
        (.setRGB timg 74 y co-white)))
    (doall
      (for [x (range 0 75)
            y (range 0 28)]
        (let [tmp-color (Color. (.getRGB img x y))
              tmp-color-num (+ (.getRed tmp-color) (.getGreen tmp-color) (.getBlue tmp-color))]
          (if (<= 100 tmp-color-num)
            (.setRGB timg x y co-white)))))
    timg))

(defn maps-to-paramstr [maps]
  (->> maps
    (map (fn [[k v]] (str (name k) "=" v)))
    (str/join "&")))
; (require '[clj-http.client :as client])
; (require '[clj-http.cookies :as cookie])
; (def my-cs (clj-http.cookies/cookie-store))
; (client/get "https://billcloud.unionpay.com/ccfront/entry/query?category=J0&insId=1900_0001" {:cookie-store my-cs})
; (client/get "https://billcloud.unionpay.com/ccfront/validate/randImg" {:cookie-store my-cs :as :byte-array})
; (:body *1)
; (ByteArrayInputStream. (:body *1))
; (ImageIO/read *1)
; (process-image *1)
; (prn (clojure.string/trim (.doOCR tess *1)))
; {"usr_num" "15248141905", "randCode" "p46h", "mchName" "内蒙古缴费一站通", "insId" "1900_0001", "usr_id_elem1" "斯琴布和", "bcode" "J0_1900_0001", "amount" "1000", "category" "J0"}
; (->> (:body *1) (ByteArrayInputStream.) (ImageIO/read) (process-image) (.doOCR tess) (clojure.string/trim))
; (def form (assoc form "randCode" "nanb" "payReq" (maps-to-paramstr form)))
; (client/post "https://billcloud.unionpay.com/ccfront/entry/createOrder" {:cookie-store my-cs :as :x-www-form-urlencoded :form-params {}})

(ImageIO/read (URL. "http://zhixing.court.gov.cn/search/captcha.do"))
(ImageIO/write img "tiff" (File. (str x ".tiff")))

(def image-url "http://zhixing.court.gov.cn/search/captcha.do")

(def x 10)

(defn get-image
  []
  (Thread/sleep 500)
  (let [img (ImageIO/read (URL. "http://zhixing.court.gov.cn/search/captcha.do"))]
    (ImageIO/write img "tiff" (File. (str x ".tiff")))
    (def x (+ x 1))))

(try (get-image) (catch Exception e (prn "Handle generic exception")))

(for [i (range 70 1000)]
  (try
    (get-image)
    (catch Exception e
      (Thread/sleep 2000)
      (prn "Handle generic exception"))))


(defn -main
  "I don't do a whole lot ... yet."
  [& args]

  (let [bimg (process-image img)]
    (prn (.doOCR tess bimg))))
    ; (ImageIO/write bimg "jpg" (File. "c.jpg"))))
  ; (prn (.doOCR tess bimg)))
  ; (prn "aaaaaa"))
    ; (for [y (range 0 28)]
      ; (prn x " " y))
      ; (if (not= co-black (.getRGB img x y))
        ; (.setRGB bimg x y co-white))))
  ; (ImageIO/write bimg "jpg" (File. "c.jpg")))
